package com.sign2024.sign2024project.goodsmaterials.controller.test;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.excel.util.ListUtils;

import java.util.List;
import java.util.Map;


public class NoModelDataListener extends AnalysisEventListener<Map<Integer, String>> {
    
    /**
     * 每隔5条存储数据库，实际使用中可以100条，然后清理list ，方便内存回收
     */
    private static final int BATCH_COUNT = 100;
    private List<Map<Integer, String>> cachedDataList = ListUtils.newArrayListWithExpectedSize(BATCH_COUNT);

    @Override
    public void invoke(Map<Integer, String> data, AnalysisContext context) {
        cachedDataList.add(data);
        if (cachedDataList.size() >= BATCH_COUNT) {
            for (Map<Integer, String> dataMap : cachedDataList) {
                for (Map.Entry<Integer, String> entry : dataMap.entrySet()) {
                    Integer key = entry.getKey();
                    String value = entry.getValue();
                    // 在这里对数据进行比对处理
                    // 对数据 进行比较
                    System.out.println("key: " + key + "-->" + " value: " + value);
                }
                System.out.println("------------------------------------");
            }

            cachedDataList = ListUtils.newArrayListWithExpectedSize(BATCH_COUNT);
        }
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext context) {
        for (Map<Integer, String> dataMap : cachedDataList) {
            for (Map.Entry<Integer, String> entry : dataMap.entrySet()) {
                Integer key = entry.getKey();
                String value = entry.getValue();
                // 在这里对数据进行比对处理
                System.out.println("key: " + key + "-->" + " value: " + value);
            }
            System.out.println("------------------------------------");
        }
    }

//    /**
//     * 加上存储数据库
//     */
//    private void saveData() {
////        log.info("{}条数据，开始存储数据库！", cachedDataList.size());
////        log.info("存储数据库成功！");
//    }
}
